package com.bartender.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bartender.common.annotation.Log;
import com.bartender.common.core.controller.BaseController;
import com.bartender.common.core.domain.AjaxResult;
import com.bartender.common.enums.BusinessType;
import com.bartender.order.domain.OrderTable;
import com.bartender.order.service.IOrderTableService;
import com.bartender.common.utils.poi.ExcelUtil;
import com.bartender.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author bartender
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/order/order")
public class OrderTableController extends BaseController
{
    @Autowired
    private IOrderTableService orderTableService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderTable orderTable)
    {
        startPage();
        List<OrderTable> list = orderTableService.selectOrderTableList(orderTable);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderTable orderTable)
    {
        List<OrderTable> list = orderTableService.selectOrderTableList(orderTable);
        ExcelUtil<OrderTable> util = new ExcelUtil<OrderTable>(OrderTable.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(orderTableService.selectOrderTableByOrderId(orderId));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderTable orderTable)
    {
        return toAjax(orderTableService.insertOrderTable(orderTable));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderTable orderTable)
    {
        return toAjax(orderTableService.updateOrderTable(orderTable));
    }

    @PutMapping("/paymentStatus")
    public AjaxResult updatePaymentStatus(@RequestParam Long orderId)
    {
        OrderTable orderTable = new OrderTable();
        orderTable.setOrderId(orderId);
        orderTable.setPaymentStatus("0");
        return toAjax(orderTableService.updateOrderTable(orderTable));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderTableService.deleteOrderTableByOrderIds(orderIds));
    }

    /**
     * 查询订单详情
     */
    @PreAuthorize("@ss.hasPermi('order:order:detail')")
    @GetMapping("/detail/{orderId}")
    public AjaxResult getOrderDetail(@PathVariable("orderId") Long orderId) {
        OrderTable orderTable = orderTableService.selectOrderTableByOrderId(orderId);
        if (orderTable.getDiyId() == null) {
            // 如果 diy_id 为空，展示酒品详情
            return success(orderTableService.selectOrderDetailByOrderId(orderId));
        } else {
            // 如果 diy_id 不为空，展示 DIY 步骤详情
            List<Map<String, Object>> diySteps = orderTableService.selectDiyDetailByDiyId(orderTable.getDiyId());
            Map<String, Object> result = new HashMap<>();
            result.put("orderInfo", orderTable);
            result.put("diySteps", diySteps);
            return success(result);
        }
    }

    @GetMapping("/allSelfOrders")
    public AjaxResult getAllOrdersByOpenid(@RequestAttribute("openid") String openid) {
        return AjaxResult.success(orderTableService.selectOrderDetailByOpenid(openid));
    }

    /**
     * 创建订单
     */
//    @PreAuthorize("@ss.hasPermi('order:order:create')")
//    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult createOrder(@RequestAttribute("openid") String openid, @RequestBody Map<String, Object> orderData, @RequestParam(value = "deptId", required = true) Long deptId) {
        return success(orderTableService.createOrder(openid, orderData, deptId));
    }

    @PostMapping("/create_diy")
    public AjaxResult createDiyOrder(@RequestAttribute("openid") String openid, @RequestParam(value = "diyName", required = true) String diyName, @RequestBody Map<String, Object> orderData, @RequestParam(value = "deptId", required = true) Long deptId) {
        return success(orderTableService.createDiyOrder(openid, diyName, orderData, deptId));
    }

    @PostMapping("/evaluate")
    public AjaxResult evaluate(@RequestAttribute("openid") String openid, @RequestParam String evaluation, @RequestParam Long orderId) {
        return success(orderTableService.updateEvaluation(evaluation, orderId));
    }

    /**
     * 获取 DIY 步骤详情
     */
    @PreAuthorize("@ss.hasPermi('order:order:detail')")
    @GetMapping("/diy/{diyId}")
    public AjaxResult getDiyDetail(@PathVariable("diyId") Long diyId) {
        // 调用服务层方法获�� DIY 步骤详情
        List<Map<String, Object>> diySteps = orderTableService.selectDiyDetailByDiyId(diyId);
        return success(diySteps);
    }
}
